/**
 * Copyright(c) 2011-2018 by YouCredit Inc.
 * All Rights Reserved
 */
package com.rrd.hive.util;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Random;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * mysql AES加密和解密
 *
 * @author Chaoqun Zheng
 */
public class AESUtil {
    public final static int ENCRYPT_LEN = 32;
    public final static int ADD_LEN = 6;
    public final static String BASE = "abcdefghijklmnopqrstuvwxyz0123456789";
    private static Pattern letterPattern = Pattern.compile("[a-zA-Z]");
//    public final static String aesKey = PropertiesUtil.getAesKey();
//    public final static String aesKeyLocal = PropertiesUtil.getLocalAesKey();

    /**
     * 加密后长度
     *
     * @param content
     * @param strKey
     * @return
     * @throws Exception
     * @throws UnsupportedEncodingException
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     */
    public static String aesEncrypt(String content, String strKey)
            throws Exception {
        byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
        SecretKey key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cleartext = content.getBytes("UTF-8");
        byte[] ciphertextBytes = cipher.doFinal(cleartext);
        return new String(Hex.encodeHex(ciphertextBytes)).toUpperCase();
    }

    public static String aesDecrypt(String content)
            throws Exception {
        String strKey = "9i9XK8If5fQdIut";
        byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
        SecretKey key = new SecretKeySpec(keyBytes, "AES");
        Cipher decipher = Cipher.getInstance("AES");
        decipher.init(Cipher.DECRYPT_MODE, key);
        char[] cleartext = content.toCharArray();
        byte[] decodeHex = Hex.decodeHex(cleartext);
        byte[] ciphertextBytes = decipher.doFinal(decodeHex);
        return new String(ciphertextBytes);
    }

    /**
     * 返回明文
     *
     * @param mobile
     * @return
     * @throws Exception
     */
    public static String decryptPhone(String mobile) throws Exception {
        if (StringUtils.isBlank(mobile)) {
            return "";
        }
        if (mobile.length() == 32) {
            return AESUtil.aesDecrypt(mobile);
        }
        return mobile;
    }

//    /**
//     * 加密手机号
//     *
//     * @param mobile
//     * @return
//     * @throws Exception
//     */
//    public static String encryptPhone(String mobile) throws Exception {
//        if (StringUtils.isBlank(mobile)) {
//            return "";
//        }
//        if (NumberUtils.isDigits(mobile) && mobile.length() == 11) { //全部是数字 并且小于20位
//            return AESUtil.aesEncrypt(mobile, AESUtil.aesKey);
//        }
//        return mobile;
//    }

    /**
     * 通过是否含有字母，判断手机号是否已加密
     * 例如
     * <ul>
     * <li>14444444444;13520208345 return false</li>
     * <li>9E839AFF57AC77240E5CB412A33D0563B2B47140500D3894B3F5488BF2E6B8E3
     * return true;</li>
     * </ul>
     *
     * @author Shuaijun He
     * @param arg
     * @return
     */
    public static boolean isEncrypted(String arg) {
        return AESUtil.letterPattern.matcher(arg).find();
    }

    /**
     * 获取n位随机字符串
     *
     * @param length
     * @return
     */
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = AESUtil.BASE;
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}